const {
  CurrencyShopModel
} = require('../../../utils/currencyshop-model.js')
const currencyShopModel = new CurrencyShopModel()
const {
  ActivityModel
} = require('../../../utils/activity-model.js')
const activityModel = new ActivityModel()
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status: '',
    shopId: '',
    orderCode: '',
    currency: {},
    paydisabled: false,
    totalMoney: {
      price: 0,
      floorPrice: 0,
      int: '0',
      float: '00'
    },
  },

  formSubmit(e) {
    this.setData({
      paydisabled: true
    })
    if (this.data.status == 2 || this.data.status == 4) {
      this.setBargainCreate()
    } else if (this.data.status == 3) {
      this.wechatPay()
    }
  },
  /**
   * 下单
   */
  setBargainCreate() {
    let that = this;
    let currency = this.data.currency;
    const merchantId = wx.getStorageSync('merchantId')
    activityModel.bargainCreate({
      merchantId: merchantId,
      shopId: currency.shopId,
      bargainStarterId: currency.activityBargainStarterId || '',
      busType: currency.productType == 1 ? 'CASH_PRODUCT_CURRENCY' : currency.productType == 2 ? 'CASH_GAME_COUPON' : ''
    }).then(data => {
      that.setData({
        orderCode: data.orderCode
      }, function () {
        if(data.orderPaySummaryList[0].amountPayable == 0 && data.orderPaySummaryList[0].priceType == 0){
          wx.reLaunch({
            url: '/activityBargain/bargain/productList/index?activityId=' + that.data.currency.activityId + '&shopId=' + that.data.currency.shopId
          })
        }else{
          that.wechatPay()
        }
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  wechatPay() {
    var that = this;
    const merchantId = wx.getStorageSync('merchantId')
    // 创建支付订单
    currencyShopModel.createPayOrder({
      merchantId,
      shopId: this.data.currency.shopId,
      orderCode: this.data.orderCode,
      channel: 'WEIXIN',
      payMethod: 'WAP_PAY'
    }).then(({
      payInfo
    }) => {
      this.setData({
        paydisabled: false
      })
      wx.requestPayment({
        timeStamp: payInfo.timeStamp,
        nonceStr: payInfo.nonceStr,
        package: payInfo.package,
        signType: payInfo.signType,
        paySign: payInfo.paySign,
        success: () => {
          wx.reLaunch({
            url: '/activityBargain/bargain/productList/index?activityId=' + this.data.currency.activityId + '&shopId=' + this.data.currency.shopId
          })
        },
        fail: () => {}
      })
    }).catch(err => {
      that.setData({
        paydisabled: false
      })
      wx.showToast({
        title: `${err.error}`,
        icon: 'none'
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const currency = JSON.parse(decodeURIComponent(options.currency))
    const payPrice = currency.floorPrice || 0
    const split = payPrice.toFixed(2).split('.')
    this.setData({
      shopId: options.shopId,
      orderCode: currency.orderCode,
      currency: currency,
      status: currency.status,
      activityId: currency.activityId,
      totalMoney: {
        price: currency.price.toFixed(2) || 0.00,
        floorPrice: payPrice,
        int: split[0],
        float: split[1]
      },
      busType: currency.productType == 1 ? 'CASH_PRODUCT_CURRENCY' : currency.productType == 2 ? 'CASH_GAME_COUPON' : ''
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let param = {
      activityId: this.data.currency.activityId || '',
      source: '砍价提交订单页面'
    }
    let merchantId = wx.getStorageSync('merchantId')
    app.setTrackerLog(merchantId, 1, 4, 1, param)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})